package movEat.GUI;

import movEat.Definitions.actorDB;
import movEat.IKernel.Operation;
import movEat.Kernel.Import.Utils;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class ActorParameters {

	private Text editActorFirstName;
	private Text editActorLastName;

	public ActorParameters(Composite editActors) {

		editActorFirstName = new Text(editActors, SWT.BORDER);
		editActorFirstName.setText("First name                ");
		editActorFirstName.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));

		editActorFirstName.addFocusListener(new FocusAdapter(){
			@Override
			public void focusGained(FocusEvent e) {
				if(editActorFirstName.getText().equals("First name                "))
					editActorFirstName.setText("");
			}
			@Override
			public void focusLost(FocusEvent e)  {
				if(editActorFirstName.getText().equals(""))
					editActorFirstName.setText("First name                ");
			}
		});

		editActorLastName = new Text(editActors, SWT.BORDER);
		editActorLastName.setText("Last name                 ");
		editActorLastName.setFont(new Font(SWTUtil.getDisplay(), "Segoe Print", 8, SWT.NONE));

		editActorLastName.addFocusListener(new FocusAdapter(){
			@Override
			public void focusGained(FocusEvent e) {
				if(editActorLastName.getText().equals("Last name                 "))
					editActorLastName.setText("");
			}
			@Override
			public void focusLost(FocusEvent e)  {
				if(editActorLastName.getText().equals(""))
					editActorLastName.setText("Last name                 ");
			}
		});


	}

	public void setEnabled(boolean b) {
		editActorFirstName.setEnabled(b);
		editActorLastName.setEnabled(b);
	}

	public String getFirstName() {
		if (editActorFirstName.getText().equals("First name                "))
			return "";
		else 
			return editActorFirstName.getText();
	}

	public String getLastName() {
		if (editActorLastName.getText().equals("Last name                 "))
			return "";
		else 
			return editActorLastName.getText();

	}

	public actorDB getActorDBFromUserInput_find(Shell shell) {
		String error = "";

		if ((getFirstName().equals("") || getFirstName().equals("First name                "))
				&& (getLastName().equals("") || getLastName().equals("Last name                 ")))
			error = "Please type a first/last name of an actor, or a part of it.\n";
		
		if(getFirstName().indexOf('_')!=-1 || getFirstName().indexOf('%')!=-1 || getLastName().indexOf('_')!=-1 || getLastName().indexOf('%')!=-1)
			error += "The name can not contain the chars '%' and '_'\n";

		if (!Utils.isValidAscii(getFirstName()) || !Utils.isValidAscii(getLastName()))
			error += "please write the name in Ascii (english for example).\n";
		
		if (!error.equals("")){
			GUI.userMessage(shell,"Not A vald Input",error);
			return null;
		}


		String firstName = getFirstName();
		String lastName = getLastName();


		if(getFirstName().equals("") || getFirstName().equals("First name                "))
			firstName="";
		if(getLastName().equals("") || getLastName().equals("Last name                 "))
			lastName="";

		return new actorDB(firstName, lastName);

	}

	public void addEnableDisableToRelatedButton(final Button b){

		editActorFirstName.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {	
				if((editActorFirstName.getText().equals("") ||editActorFirstName.getText().equals("First name                "))
						&&
						(editActorLastName.getText().equals("") ||editActorLastName.getText().equals("Last name                 "))){
					b.setEnabled(false);
					b.setGrayed(true);
				}
				else{
					b.setEnabled(true);
					b.setGrayed(false);
				}
			}		
		});

		editActorLastName.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {	
				if((editActorFirstName.getText().equals("") ||editActorFirstName.getText().equals("First name                "))
						&&
						(editActorLastName.getText().equals("") ||editActorLastName.getText().equals("Last name                 "))){
					b.setEnabled(false);
					b.setGrayed(true);
				}
				else{
					b.setEnabled(true);
					b.setGrayed(false);
				}
			}		
		});


	}
}
